<template>
  <div class="sensitiveWords">
    <search-block>
      <template slot="button">
        <el-button type="primary" @click="search(true)">查询</el-button>
        <el-button type="primary" @click="addWords">添加敏感词</el-button>
      </template>
      <el-form inline :model="model" label-width="100px" @submit.native.prevent>
        <el-form-item label="词汇名称:">
          <el-input v-model="model.words" clearable></el-input>
        </el-form-item>
      </el-form>
    </search-block>
    <y-table
      ref="table"
      border
      pagination
      :getDataWay="getDataWay"
      :columns="columns">
      <el-table-column slot="y-oper" label="操作" align="center" header-align="center" width="160">
        <template v-slot="{row}">
          <div class="table-buttons">
            <el-button type="primary" size="mini" @click="editWords(row)">编辑</el-button>
            <el-popconfirm
                title="是否确认删除该敏感词？"
                @confirm="delWords(row)">
              <el-button slot="reference" type="danger" size="mini">删除</el-button>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </y-table>
    <y-form
      ref="yForm"
      v-model="forms.formModel"
      :visible.sync="forms.showForm"
      :title="forms.formTypeTitle[forms.formsType]"
      destroy-on-close
      width="50%"
      :form-attrs="forms.formAttrs"
      :columns="formColumns"
      @ensure="ensureModel">
    </y-form>
  </div>
</template>

<script>
export default {
  name: "sensitiveWords",
  inject: ['getDictNameSync', 'generateMediaUrl'],
  computed:{
    getDataWay(){
      return{
        dataType: 'url',
        data: this.$api.system.sensitiveWordsPage,
        param: this.model
      }
    },
    formColumns(){
      return [
        {
          label: "敏感词",
          prop: "words",
          comp:{
            compType: 'input',
            attrs:{
              maxlength: 50,
              showWordLimit: true
            }
          }
        }
      ];
    }
  },
  data(){
    return{
      model:{
        words: ''
      },
      columns: [
        {
          label: "敏感词",
          prop: "words"
        }
      ],
      forms:{
        formsType: '',
        formTypeTitle: {
          'ADD': '添加敏感词',
          'EDIT': '编辑敏感词'
        },
        formModel:{
          words: ''
        },
        currentForm: null,
        showForm: false,
        formAttrs:{
          labelWidth: '100px',
          rules: {
            words: [{required: true, message: "请填写敏感词", trigger: "blur"}]
          }
        }
      }
    }
  },
  methods:{
    resetForm(){
      Object.keys(this.forms.formModel).map(key => {
        this.forms.formModel[key] = this.forms.currentForm ? this.forms.currentForm[key] : '';
      })
    },
    search(jumpToFirst){
      this.$refs['table'].loadData(this.model, jumpToFirst);
    },
    addWords(){
      this.forms.formsType = 'ADD';
      this.forms.currentForm = null;
      this.resetForm();
      this.forms.showForm = true;
    },
    editWords(row){
      this.forms.formsType = 'EDIT';
      this.forms.currentForm = row;
      this.resetForm();
      this.forms.showForm = true;
    },
    delWords(row){
      this.$yAxios({
        url: this.$api.system.removeSensitiveWords,
        data:{
          ids: [row.id]
        }
      }, info => {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.search();
      })
    },
    ensureModel(form){
      let newForm;
      if(this.forms.formsType === 'EDIT'){
        newForm = Object.assign({}, this.forms.currentForm, form);
      }else{
        newForm = Object.assign({}, form);
      }
      this.$yAxios({
        url: this.$api.system.saveSensitiveWords,
        data: newForm
      }, info => {
        this.$message({
          type: 'success',
          message: '保存成功'
        });
        this.search(this.forms.formsType === 'ADD');
      })
    },
  }
}
</script>

<style scoped>

</style>